package com.app.controller;

import com.app.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: cn
 * @Description: TODO
 * @Date: 2025/7/12 13:45
 * @Version: 1.0
 */
@RestController
@RequestMapping("/test")
public class JwtTestController {
    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 获取token，这个是测试接口，直接获取
     * @return
     */
    @GetMapping("/generate-token")
    public String generateToken() {
        UserDetails user = User.withDefaultPasswordEncoder()
                .username("admin")
                .password("secret")
                .roles("USER")
                .build();

        return jwtUtil.generateToken(user);
    }

    @GetMapping("/validate-token")
    public boolean validateToken(@RequestParam String token) {
        UserDetails user = User.withDefaultPasswordEncoder()
                .username("admin")
                .password("secret")
                .roles("USER")
                .build();

        return jwtUtil.validateToken(token, user);
    }
}
